package com.fhnew.app.ui.activity.userinfo;


import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.fhnew.app.BuildConfig;
import com.fhnew.app.R;
import com.fhnew.app.model.internet.model.UserModel;


import com.fhnew.app.ui.activity.userinfo.fragment.LotteryListFragment;
import com.fhnew.app.ui.activity.userinfo.fragment.yue.CashListFragment;
import com.fhnew.app.ui.activity.userinfo.fragment.yue.ChargeDialogFragment;
import com.fhnew.app.ui.activity.userinfo.fragment.yue.ChargeMoneyFragment;
import com.fhnew.app.ui.activity.userinfo.fragment.yue.HelpFragment;
import com.fhnew.app.ui.activity.userinfo.fragment.YuEFragment;
import com.fhnew.app.ui.dialogactivity.SetingActivity;
import com.fhnew.app.ui.activity.userinfo.fragment.CardFragment;
import com.fhnew.app.ui.activity.userinfo.fragment.ChangemoneyFragment;

import com.fhnew.app.ui.activity.userinfo.fragment.MycodeFragment;
import com.fhnew.app.ui.activity.userinfo.fragment.SafecenterFragment;
import com.fhnew.app.ui.activity.userinfo.fragment.StationletterFragment;
import com.fhnew.app.ui.activity.userinfo.fragment.UserdataFragment;
import com.fhnew.app.ui.activity.userinfo.fragment.UsermenueFragment;
import com.fhnew.app.util.ActivityUtil;
import com.fhnew.app.util.EventBus.ReloginEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;


/**
 * 个人中心
 */
public class ActivityData extends AppCompatActivity implements PageDataChangeControl {

    private ImageView reback;
    private ImageView shezhi;
    private TextView gerenzhongxin;
    private TextView zhanghuqingdan;
    private TextView mymoney;
    private TextView mydata;
    private TextView mysafe;
    private TextView mycard;
    private TextView mymaneage;
    private TextView lotteryList;
    private TextView textView_Yue;

    private TextView title;
    private ViewGroup fullScreenView;

    private ScrollView selectList;
    private View selectLayout01, selectLayout02, selectLayout03, selectLayout04,
            selectLayout05, selectLayout06, selectLayout07, selectLayout08,selectLayout09;

    private Fragment mycodeFragment;//个人中心
    private YuEFragment yuEFragment;//余额宝
    private CashListFragment cashListFragment;
    private ChargeMoneyFragment chargeMoneyFragment;
    private HelpFragment helpFragment;
    private LotteryListFragment lotteryListFragment;
    private Fragment userdataFragment ;//个人资料
    private Fragment usermenueFragment ;//账户清单
    private Fragment changemoneyFragment ;//额度转换
    private Fragment safecenterFragment ;//安全中心
    private Fragment cardFragment ;//银行卡管理
    private Fragment stationletterFragment;//站内信

    private ChargeDialogFragment chargeDialogFragment;

    private Fragment actionFragment;
    private TextView actionView;

    private boolean isfirstload = true;
    private String str;

    private void initView () {

//        if (!EventBus.getDefault().isRegistered(this)) {
//            EventBus.getDefault().register(this);
//        }

        fullScreenView = findViewById(R.id.relativeLayout_Full_Content);

        selectList = findViewById(R.id.scrollView_UserCenter_SelectList);

        reback = findViewById(R.id.reback);
        shezhi = findViewById(R.id.shezhi);
        gerenzhongxin = findViewById(R.id.gerenzhongxin);
        zhanghuqingdan = findViewById(R.id.zhanghuqingdan);
        mymoney = findViewById(R.id.mymoney);
        mydata = findViewById(R.id.mydata);
        mysafe = findViewById(R.id.mysafe);
        mycard = findViewById(R.id.mycard);
        mymaneage = findViewById(R.id.mymaneage);
        lotteryList = findViewById(R.id.lotteryList);
        textView_Yue = findViewById(R.id.textView_yuebao);

        reback.setOnClickListener(onClickListener);
        shezhi.setOnClickListener(onClickListener);

        title = findViewById(R.id.textView_PersonalCenter_Title);

        selectLayout01 = findViewById(R.id.view_UserCenter_Select01);
        selectLayout02 = findViewById(R.id.view_UserCenter_Select02);
        selectLayout03 = findViewById(R.id.view_UserCenter_Select03);
        selectLayout04 = findViewById(R.id.view_UserCenter_Select04);
        selectLayout05 = findViewById(R.id.view_UserCenter_Select05);
        selectLayout06 = findViewById(R.id.view_UserCenter_Select06);
        selectLayout07 = findViewById(R.id.view_UserCenter_Select07);
        selectLayout08 = findViewById(R.id.view_UserCenter_Select08);
        selectLayout09 = findViewById(R.id.view_UserCenter_Select09);

        selectLayout01.setOnClickListener(onSelectyListener);
        selectLayout02.setOnClickListener(onSelectyListener);
        selectLayout03.setOnClickListener(onSelectyListener);
        selectLayout04.setOnClickListener(onSelectyListener);
        selectLayout05.setOnClickListener(onSelectyListener);
        selectLayout06.setOnClickListener(onSelectyListener);
        selectLayout07.setOnClickListener(onSelectyListener);
        selectLayout08.setOnClickListener(onSelectyListener);
        selectLayout09.setOnClickListener(onSelectyListener);

        //开发过程中万盛和富豪的区别
        if (BuildConfig.CASH) {
            selectLayout02.setVisibility(View.VISIBLE);
        } else {
            selectLayout02.setVisibility(View.GONE);
        }
    }

    public void initFragment () {
        if (!TextUtils.isEmpty(str)) {
            if (str.equals("清单")) {
                changeFragmemt("usermenue", (Bundle) null);
            } else if (str.equals("绑卡")) {
                changeFragmemt("card", (Bundle) null);
            } else if (str.equals("密码")) {
                changeFragmemt("safecenter", (Bundle) null);
            } else {
                selectList.scrollTo(0,selectLayout03.getTop());
                changeFragmemt("stationletter", (Bundle) null);
            }
        } else {
            changeFragmemt("mycode", (Bundle) null);
        }
    }



    public void changeFragmemt (String pageTag, Bundle bundle) {
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();

        if(actionView != null)
            actionView.setBackgroundResource(R.mipmap.gameup);

        if(chargeDialogFragment != null){
            fullScreenView.setVisibility(View.GONE);
        }

        if(actionFragment != null)
            ft.hide(actionFragment);


        if(bundle == null){
            bundle = new Bundle();
        }
        switch (pageTag) {
            case "mycode":
                title.setText("个人中心     Personal center");
                if(mycodeFragment == null){
                    mycodeFragment = new MycodeFragment();
                    ft.add(R.id.content, mycodeFragment, "mycode");
                }
                actionView = gerenzhongxin;
                actionFragment = mycodeFragment;
                break;
            case "YuE":
                title.setText("余额宝     Yu’E Bao");
                if (yuEFragment == null) {
                    yuEFragment = new YuEFragment();
                    yuEFragment.setListener(this);
                    ft.add(R.id.content, yuEFragment, "YuE");
                }
                actionFragment = yuEFragment;
                actionView = textView_Yue;
                break;
            case "YuE_01":
                if (chargeDialogFragment == null) {
                    chargeDialogFragment = new ChargeDialogFragment(fullScreenView);
                    chargeDialogFragment.setListener(this);
                }
                fullScreenView.setVisibility(View.VISIBLE);
                chargeDialogFragment.dataChanged(bundle);
                break;
            case "CashList":
                title.setText("余额宝     Yu’E Bao");
                if (cashListFragment == null) {
                    cashListFragment = new CashListFragment();
                    cashListFragment.setListener(this);
                    ft.add(R.id.content, cashListFragment, "CashList");
                }
                cashListFragment.dataChanged(bundle);
                actionFragment = cashListFragment;
                actionView = textView_Yue;
                break;
            case "Charge":
                title.setText("余额宝     Yu’E Bao");
                if (chargeMoneyFragment == null) {
                    chargeMoneyFragment = new ChargeMoneyFragment();
                    chargeMoneyFragment.setListener(this);
                    ft.add(R.id.content, chargeMoneyFragment, "Charge");
                }
                chargeMoneyFragment.dataChanged(bundle);
                actionFragment = chargeMoneyFragment;
                actionView = textView_Yue;
                break;
            case "Help":
                title.setText("余额宝     Yu’E Bao");
                if (helpFragment == null) {
                    helpFragment = new HelpFragment();
                    helpFragment.setListener(this);
                    ft.add(R.id.content, helpFragment, "Help");
                }
                helpFragment.dataChanged(bundle);
                actionFragment = helpFragment;
                actionView = textView_Yue;
                break;
            case "lotteryList":
                title.setText("投注记录     Lottery betting records");
                if (lotteryListFragment == null) {
                    lotteryListFragment = new LotteryListFragment();
                    ft.add(R.id.content, lotteryListFragment, "lotteryList");
                }
                actionFragment = lotteryListFragment;
                actionView = lotteryList;
                break;
            case "usermenue":
                title.setText("账户清单     Account list");
                if (usermenueFragment == null) {
                    usermenueFragment = new UsermenueFragment();
                    ft.add(R.id.content, usermenueFragment, "usermenue");
                }
                actionFragment = usermenueFragment;
                actionView = zhanghuqingdan;
                break;
            case "changemoney":
                title.setText("额度转换     Credit conversion");
                if (changemoneyFragment == null) {
                    changemoneyFragment = new ChangemoneyFragment();
                    ft.add(R.id.content, changemoneyFragment, "changemoney");
                }
                actionFragment = changemoneyFragment;
                actionView = mymoney;
                break;
            case "userdata":
                title.setText("个人资料     Personal information");
                if(userdataFragment == null){
                    userdataFragment = new UserdataFragment();
                    ft.add(R.id.content, userdataFragment, "PI");
                }
                actionFragment = userdataFragment;
                actionView = mydata;
                break;
            case "safecenter":
                title.setText("安全中心     Security center");
                if (safecenterFragment == null) {
                    safecenterFragment = new SafecenterFragment();
                    ft.add(R.id.content, safecenterFragment, "safecenter");
                }
                actionFragment = safecenterFragment;
                actionView = mysafe;
                break;
            case "card":
                title.setText("银行卡管理     Bank card management");
                if (cardFragment == null) {
                    cardFragment = new CardFragment();
                    ft.add(R.id.content, cardFragment, "card");
                }
                actionFragment = cardFragment;
                actionView = mycard;
                break;
            case "stationletter":
                title.setText("站内信     Station letter");
                if (stationletterFragment == null) {
                    stationletterFragment = new StationletterFragment();
                    ft.add(R.id.content, stationletterFragment, "stationletter");
                }
                actionFragment = stationletterFragment;
                actionView = mymaneage;
                break;
            case "finish":
                finish();
                break;
            case "Back":
                if(actionFragment!= null && actionFragment instanceof FragmentDataChang)
                    ((FragmentDataChang)actionFragment).dataChanged(bundle);
                break;
        }

        if(actionView != null)
            actionView.setBackgroundResource(R.mipmap.gamedown);

        if(actionFragment != null)
            ft.show(actionFragment);

        ft.commitAllowingStateLoss();
    }

    @Override
    protected void onCreate (Bundle savedInstanceState) {
        ActivityUtil.fullScreen(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user_data);

        str = getIntent().getStringExtra("intentm");

        initView();

        init();
    }

    @Override
    protected void onResume () {
        super.onResume();
    }

    @Override
    protected void onPause () {
        super.onPause();
    }

    @Override
    protected void onDestroy () {
        super.onDestroy();
        userModel.unRegistListener(userInformationChanged);
//        EventBus.getDefault().unregister(this);
    }
//    @Subscribe(threadMode = ThreadMode.MAIN)
//    public void Event (ReloginEvent reloginEvent) {
//        if (reloginEvent.getMessage().equals("200")) {
//            changeFragmemt("safecenter", (Bundle) null);
//        }
//    }
    @Override
    public void onWindowFocusChanged (boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            if (isfirstload) {
                isfirstload = false;
                initFragment();
            }
        }
    }

    private View.OnClickListener onSelectyListener = new View.OnClickListener() {
        @Override
        public void onClick (View v) {
            int id = v.getId();
            switch (id) {
                case R.id.view_UserCenter_Select01:
                    changeFragmemt("mycode", (Bundle) null);
                    break;
                case R.id.view_UserCenter_Select02:
                    changeFragmemt("YuE", (Bundle) null);
                    break;
                case R.id.view_UserCenter_Select03:
                    changeFragmemt("usermenue", (Bundle) null);
                    break;
                case R.id.view_UserCenter_Select04:
                    changeFragmemt("changemoney", (Bundle) null);
                    break;
                case R.id.view_UserCenter_Select05:
                    changeFragmemt("userdata", (Bundle) null);
                    break;
                case R.id.view_UserCenter_Select06:
                    changeFragmemt("safecenter", (Bundle) null);
                    break;
                case R.id.view_UserCenter_Select07:
                    changeFragmemt("card", (Bundle) null);
                    break;
                case R.id.view_UserCenter_Select08:
                    changeFragmemt("stationletter", (Bundle) null);
                    break;
                case R.id.view_UserCenter_Select09:
                    changeFragmemt("lotteryList", (Bundle) null);
                    break;
            }
        }
    };

    private View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick (View view) {
            switch (view.getId()) {
                case R.id.reback:
                    finish();
                    break;
                case R.id.shezhi:
                    finish();
                    Intent intent = new Intent(ActivityData.this, SetingActivity.class);
                    startActivity(intent);
                    break;
            }
        }
    };

    private UserModel.UserInformationChanged userInformationChanged = new UserModel.UserInformationChanged() {
        @Override
        public void userInformationChanged () {
            if (!userModel.isLogin()){
                Toast.makeText(ActivityData.this, "登录失效", Toast.LENGTH_SHORT).show();
                finish();
            }
        }
    };


    //============================================================================================
    //Model
    //============================================================================================

    private UserModel userModel;

    private void init () {
        userModel = UserModel.getInstance(this);
        userModel.registListener(userInformationChanged);
    }


}
